home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 14
/
Mac Magazin and MacEasy Magazine CD - Issue 14.iso
/
Wissenschaft & Technik
/
Tools Plus 2.6.1 Evaluation Kit
/
Tools Plus 2.6.1
/
Tutorials
/
3-List Boxes
/
Tutorial.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-01
|
7KB
|
221 lines
// Tools Plus Tutorial -- List Boxes
#include "ToolsPlus.h"
#define LeftList 1 // Constants to make code more readable…
#define RightList 2
#define MoveButton 1
#define RemoveButton 2
#define DoneButton 255
#define LeftToRight 1
#define RightToLeft -1
TPPollRecord Poll; // Polling record to retrieve event information
Boolean ExitTheDemo; // Should the demo terminate?
short Direction; // Direction in which lines are moved (left-to-right
// or right-to-left)
void ApplicationInitialization (void);
void ActionInWindow (void);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void main(void)
{
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
MaxApplZone();
if (!InitToolsPlus(1, 1, UseColor))
ExitToShell();
ApplicationInitialization();
while (!ExitTheDemo) //Main Event Loop
if (PollSystem(&Poll)) //If an event is available, process the event…
if ((Poll.What == doListBox) || (Poll.What == doButton)) // User clicked something in this window
ActionInWindow();
// All other events are ignored
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplicationInitialization (void)
{
// Do all the application setup before you start polling for events…
#define DemoWindow1 1
FontInfo Font; // Info about current font
short BoxHeight; // List box's height in pixels
short resCount; // Number of resources
short resIndex; // Resource index counter
Handle hRsrc; // Handle to a resource
short theResID; // Resource's ID
ResType theResType; // Resource's Type
Str255 theResName; // Resource's Name
short InsertLineAt; // Location where line should be inserted in list to be
// in alphabetic order
WindowOpen(DemoWindow1, 0, 0, 440, 130, "\p", dBoxProc + wCenter, NoGoAway, Modal);
// Create left list box, making it exactly 6 lines high using Chicago 12pt…
TextFont(systemFont);
TextSize(12);
GetFontInfo(&Font);
BoxHeight = 6 * (Font.ascent + Font.descent + Font.leading);
NewListBox(LeftList, 15, 15, 145, 15 + BoxHeight, lOnlyOne);
// Create left list box, making it exactly 8 lines high using Geneva 9pt…
TextFont(geneva);
TextSize(9);
GetFontInfo(&Font);
BoxHeight = 8 * (Font.ascent + Font.descent + Font.leading);
NewListBox(RightList, 280, 15, 410, 15 + BoxHeight, lOnlyOne);
// Create the buttons used to control these lists…
NewButton(MoveButton, 175, 20, 265, 40, "\pMove", pushButProc, disabled, notSelected);
NewButton(RemoveButton, 175, 50, 265, 70, "\pRemove", pushButProc, disabled, notSelected);
NewButton(DoneButton, 185, 92, 255, 112, "\pDone", pushButProc, enabled, notSelected);
// Populate the left list with the names of all the fonts. We are assuming that
// you are using System 6 or later, which uses 'FOND' resourses for fonts.
DrawListBox(LeftList, false); // Stop list box drawing for faster speed
SetResLoad(false); // Don't load resources for more speed
resCount = CountResources('FOND'); // How many fonts are there?
for (resIndex = 1; resIndex <= resCount; resIndex++)
{
hRsrc = GetIndResource('FOND', resIndex); // Get a handle to the font
GetResInfo(hRsrc, &theResID, &theResType, theResName);// Get the font's ID, Type and Name
InsertLineAt = SearchListBox(LeftList, theResName); // Where should this name go?
InsertListBoxLine(LeftList, InsertLineAt); // Insert an empty line
SetListBoxText(LeftList, InsertLineAt, theResName); // Put font name into empty line
}
SetResLoad(true); // Resume loading resources
DrawListBox(LeftList, true); // Draw the list box's contents
Direction = none;
ExitTheDemo = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ActionInWindow (void)
{
short SourceList, TargetList; // Source and target list when moving lines
short SourceLine, TargetLine; // Line number in source and target list
Str255 FontName;
CurrentWindow(1); // Subsequent actions affect window #1 (our only window)
switch (Poll.What)
{
case doListBox:
if (GetListBoxLines(Poll.ListBox.Num, 1) == none) // If an empty line was clicked…
{ // Nothing selected, so disable the buttons and deselect lines in both lists…
Direction = none;
ButtonTitle(MoveButton, "\pMove");
EnableButton(MoveButton, off);
EnableButton(RemoveButton, off);
SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
}
else
{ // If a non-empty line was clicked in the list box…
switch (Poll.ListBox.Num)
{
case LeftList:
Direction = LeftToRight;
ButtonTitle(MoveButton, "\p>> Move >>");
// Turn off the line in the other list (if it has one selected)…
SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
break;
case RightList:
Direction = RightToLeft;
ButtonTitle(MoveButton, "\p<< Move <<");
// Turn off the line in the other list (if it has one selected)…
SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
break;
}
EnableButton(MoveButton, on);
EnableButton(RemoveButton, on);
}
break;
case doButton:
switch (Poll.Button.Num)
{
case MoveButton: // Move line from source list to target list…
if (Direction == LeftToRight)
{
SourceList = LeftList;
TargetList = RightList;
ButtonTitle(MoveButton, "\p<< Move <<");
}
else
{
SourceList = RightList;
TargetList = LeftList;
ButtonTitle(MoveButton, "\p>> Move >>");
}
SourceLine = GetListBoxLines(SourceList, 1); // Find selected line in source list
GetListBoxText(SourceList, SourceLine, FontName); // Get selected line's text
DeleteListBoxLine(SourceList, SourceLine); // Delete line in source list
TargetLine = SearchListBox(TargetList, FontName); // Where should this name go in target list?
InsertListBoxLine(TargetList, TargetLine); // Insert an empty line
SetListBoxText(TargetList, TargetLine, FontName); // Put font name into empty line
SetListBoxLine(TargetList, TargetLine, on); // Select line in target list
Direction = -Direction; // Change direction of movement
break;
case RemoveButton: // Remove the selected line…
if (Direction == LeftToRight)
SourceList = LeftList;
else
SourceList = RightList;
DeleteListBoxLine(SourceList, GetListBoxLines(SourceList, 1));
Direction = none;
ButtonTitle(MoveButton, "\pMove");
EnableButton(MoveButton, off);
EnableButton(RemoveButton, off);
break;
case DoneButton: // We're done, quit the application…
ExitTheDemo = true;
break;
}
break;
}
}